#include "turret.h"

#include <QPainter>
#include <QPoint>
#include <QDebug>
#include "game.h"
#include "enemy.h"
//#include <bits/stdc++.h>
#include <QVector2D>
#include <QtMath>
Turret::Turret(QPoint pos,int type,Game *game)
{
    this->pos=pos;
    this->type=type;
    this->game=game;
    //隔一段时间发子弹
    //timer = new QTimer(this);
    //connect(timer,SIGNAL(timeout()),this,SLOT(attack()));
    //timer->start(firerate*1000);

    switch(type)
    {
    case 1 :
        {
            sprite.load(":/img/turret.png");
            maxHp=100;
            currentHp=maxHp;
            range=100;
            firerate=20;
            radius=30;
            power=100;
            break;
        }
    case 2 :
    {
        sprite.load(":/img/turret2.png");
        maxHp=120;
        currentHp=maxHp;
        range=100;
        firerate=40;
        radius=30;
        power=120;
        break;
    }
    case 3 :
    {
        sprite.load(":/img/turret3.png");
        maxHp=120;
        currentHp=maxHp;
        range=150;
        firerate=80;
        radius=30;
        power=130;
        break;
    }
    }
    qDebug()<<"second";
}

Turret::~Turret()
{

}

void Turret::draw(QPainter *painter) const
{
    painter->save();
    painter->resetTransform();
    QPoint drawPos(-sprite.width()/2,-sprite.height()/2);
    painter->translate(pos);
    painter->rotate(rotationSprite);
    painter->drawPixmap(drawPos,sprite);
    //边框
    QPoint mousePos=QCursor::pos()-game->mapToGlobal(QPoint(0,0));
    if(sqrt(mousePos.x()-pos.x())
            +sqrt(mousePos.y()-pos.y())
            <sqrt(radius))
    {
        QPoint drawP(0,0);
        painter->setPen(QColor(255,255,255));
        painter->setBrush(QBrush(QColor(255,255,255,100)));
        painter->drawEllipse(drawP,range,range);
    }

    painter->resetTransform();
    painter->restore();
}
bool Turret::check_enemy_range(Game *game)
{
    current_enemy=NULL;
    int flag=0;
    enemy_set.clear();
    for(int i=0;i<game->enemy_list.size();i++)
    {
        Enemy *enemy=game->enemy_list.at(i);
        int ex=enemy->pos.x();
        int ey=enemy->pos.y();
        int nx=pos.x();
        int ny=pos.y();
        if(((ex-nx)*(ex-nx)+(ey-ny)*(ey-ny))<range*range)
        {
            flag=1;
            enemy_set.insert(enemy);
        }
    }
    if(flag==0) return false;
    current_enemy=*(enemy_set.begin());
    if (current_enemy)
    {
        // 这种情况下,需要旋转炮台对准敌人
        // 向量标准化
        QVector2D normalized(current_enemy->pos - pos);
        normalized.normalize();
        rotationSprite = qRadiansToDegrees(qAtan2(normalized.y(), normalized.x()))+90;

        // 如果敌人脱离攻击范围
        //if (!collisionWithCircle(m_pos, m_attackRange, m_chooseEnemy->pos(), 1))
        //	lostSightOfEnemy();
        //qDebug()<<"rotate="<<rotationSprite;
    }
    return true;
}

void Turret::setHp(int hurt)
{
    this->currentHp-=hurt;
    if(this->currentHp<=0)
    {
        this->game->remove_turret(this);
    }
}

void Turret::attack()
{
    if(atktime==firerate){
        atktime=0;
        //qDebug()<<"attack one bullet"<<current_enemy->pos;
    if(this->game->check_enemy(current_enemy)){

        this->game->add_bullet(power,type-1,0,game,this->pos,current_enemy,0);
    }
    }
    else atktime++;
}

bool Turret::levelup(int id) //0升级威力， 1升级范围  ，2 升级攻击频率   3，修理  成功return 1 失败 0
{

    int init_price=100; //第一次升级价格，暂定为100
    switch(id)
    {
        case 0:
        {
            if(this->uplevel_power>3) return false;
            for(int i=1;i<=this->uplevel_power;i++){
                init_price*=1.5;
            }
            this->uplevel_power++;
            if(this->game->pay_money(init_price)){
                this->power*=1.5;
                return true;
            }
            else return false;
        }
        case 1:
        {
            if(this->uplevel_range>3) return false;
            for(int i=1;i<=this->uplevel_range;i++){
                init_price*=1.5;
            }
            this->uplevel_range++;
            if(this->game->pay_money(init_price)){
                this->range*=1.5;
                return true;
            }
            else return false;
        }
        case 2:
        {
            if(this->uplevel_firerate>3) return false;
            for(int i=1;i<=this->uplevel_firerate;i++){
                init_price*=1.5;
            }
            this->uplevel_firerate++;
            if(this->game->pay_money(init_price)){
                this->firerate/=1.5;
                return true;
            }
            else return false;
        }
        case 3:
        {
            repair();
            return true;
        }
    }
        return 1;
}

QPoint Turret::getPos()
{
    return pos;
}

void Turret::repair()
{
    int price=1; //回一格血所需要的价格,暂设置为1
    if(this->game->pay_money((this->maxHp-this->currentHp)*price))
    {
        this->currentHp=this->maxHp;
    }
    else
    {
        int Hp; //剩余的钱可回血量
        Hp=this->game->money/price;
        this->game->pay_money(Hp*price);
        this->currentHp+=Hp;
    }
}

void Turret::UpdateMap()
{
    check_enemy_range(game);
    attack();
}
